<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>Linux 命令- mv | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>Linux 命令- mv</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Linux命令/">Linux命令</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;mv命令是move的缩写，可以用来移动文件或者将文件改名（move (rename) files），是Linux系统下常用的命令，经常用来备份文件或者目录。</p>
<h2 id="1．命令格式："><a href="#1．命令格式：" class="headerlink" title="1．命令格式："></a>1．命令格式：</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mv [选项] [源文件或目录] [目标文件或目录]</div></pre></td></tr></table></figure>
<h2 id="2．命令功能："><a href="#2．命令功能：" class="headerlink" title="2．命令功能："></a>2．命令功能：</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;视mv命令中第二个参数类型的不同（是目标文件还是目标目录），mv命令将文件重命名或将其移至一个新的目录中。当第二个参数类型是文件时，mv命令完成文件重命名，此时，源文件只能有一个（也可以是源目录名），它将所给的源文件或目录重命名为给定的目标文件名。当第二个参数是已存在的目录名称时，源文件或目录参数可以有多个，mv命令将各参数指定的源文件均移至目标目录中。在跨文件系统移动文件时，mv先拷贝，再将原有文件删除，而链至该文件的链接也将丢失。</p>
<h2 id="3．命令参数："><a href="#3．命令参数：" class="headerlink" title="3．命令参数："></a>3．命令参数：</h2><ul>
<li>-b ：若需覆盖文件，则覆盖前先行备份。  </li>
<li>-f ：force 强制的意思，如果目标文件已经存在，不会询问而直接覆盖；</li>
<li>-i ：若目标文件 (destination) 已经存在时，就会询问是否覆盖！</li>
<li>-u ：若目标文件已经存在，且 source 比较新，才会更新(update)  </li>
<li>-t  ： –target-directory=DIRECTORY move all SOURCE arguments into DIRECTORY，即指定mv的目标目录，该选项适用于移动多个源文件到一个目录的情况，此时目标目录在前，源文件在后。     </li>
</ul>
<h2 id="4．命令实例："><a href="#4．命令实例：" class="headerlink" title="4．命令实例："></a>4．命令实例：</h2><h3 id="实例1：文件改名"><a href="#实例1：文件改名" class="headerlink" title="实例1：文件改名"></a>实例1：文件改名</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mv test.log test1.txt</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">总计 20drwxr-xr-x 6 root root 4096 10-27 01:58 scf</div><div class="line">drwxrwxrwx 2 root root 4096 10-25 17:46 test3</div><div class="line">drwxr-xr-x 2 root root 4096 10-25 17:56 test4</div><div class="line">drwxr-xr-x 3 root root 4096 10-25 17:56 test5</div><div class="line">-rw-r--r-- 1 root root   16 10-28 06:04 test.log</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># mv test.log test1.txt</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">总计 20drwxr-xr-x 6 root root 4096 10-27 01:58 scf</div><div class="line">-rw-r--r-- 1 root root   16 10-28 06:04 test1.txt</div><div class="line">drwxrwxrwx 2 root root 4096 10-25 17:46 test3</div><div class="line">drwxr-xr-x 2 root root 4096 10-25 17:56 test4</div><div class="line">drwxr-xr-x 3 root root 4096 10-25 17:56 test5</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明：</strong><br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;将文件test.log重命名为test1.txt     </p>
<h3 id="实例2：移动文件"><a href="#实例2：移动文件" class="headerlink" title="实例2：移动文件"></a>实例2：移动文件</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mv test1.txt test3</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">总计 20drwxr-xr-x 6 root root 4096 10-27 01:58 scf</div><div class="line">-rw-r--r-- 1 root root   29 10-28 06:05 test1.txt</div><div class="line">drwxrwxrwx 2 root root 4096 10-25 17:46 test3</div><div class="line">drwxr-xr-x 2 root root 4096 10-25 17:56 test4</div><div class="line">drwxr-xr-x 3 root root 4096 10-25 17:56 test5</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># mv test1.txt test3</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">总计 16drwxr-xr-x 6 root root 4096 10-27 01:58 scf</div><div class="line">drwxrwxrwx 2 root root 4096 10-28 06:09 test3</div><div class="line">drwxr-xr-x 2 root root 4096 10-25 17:56 test4</div><div class="line">drwxr-xr-x 3 root root 4096 10-25 17:56 test5</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cd test3</span></div><div class="line">[root@localhost test3]<span class="comment"># ll</span></div><div class="line">总计 4</div><div class="line">-rw-r--r-- 1 root root 29 10-28 06:05 test1.txt</div><div class="line">[root@localhost test3]<span class="comment">#</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明：</strong><br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;将test1.txt文件移到目录test3中    </p>
<h3 id="实例3：将文件log1-txt-log2-txt-log3-txt移动到目录test3中。"><a href="#实例3：将文件log1-txt-log2-txt-log3-txt移动到目录test3中。" class="headerlink" title="实例3：将文件log1.txt,log2.txt,log3.txt移动到目录test3中。"></a>实例3：将文件log1.txt,log2.txt,log3.txt移动到目录test3中。</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">mv log1.txt log2.txt log3.txt test3</div><div class="line">mv -t /opt/soft/<span class="built_in">test</span>/test4/ log1.txt log2.txt 	log3.txt</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">总计 28</div><div class="line">-rw-r--r-- 1 root root    8 10-28 06:15 log1.txt</div><div class="line">-rw-r--r-- 1 root root   12 10-28 06:15 log2.txt</div><div class="line">-rw-r--r-- 1 root root   13 10-28 06:16 log3.txt</div><div class="line">drwxrwxrwx 2 root root 4096 10-28 06:09 test3</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># mv log1.txt log2.txt log3.txt test3</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">总计 16drwxrwxrwx 2 root root 4096 10-28 06:18 test3</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cd test3/</span></div><div class="line">[root@localhost test3]<span class="comment"># ll</span></div><div class="line">总计 16</div><div class="line">-rw-r--r-- 1 root root  8 10-28 06:15 log1.txt</div><div class="line">-rw-r--r-- 1 root root 12 10-28 06:15 log2.txt</div><div class="line">-rw-r--r-- 1 root root 13 10-28 06:16 log3.txt</div><div class="line">-rw-r--r-- 1 root root 29 10-28 06:05 test1.txt</div><div class="line">[root@localhost test3]<span class="comment">#</span></div><div class="line">[root@localhost test3]<span class="comment"># ll</span></div><div class="line">总计 20</div><div class="line">-rw-r--r-- 1 root root    8 10-28 06:15 log1.txt</div><div class="line">-rw-r--r-- 1 root root   12 10-28 06:15 log2.txt</div><div class="line">-rw-r--r-- 1 root root   13 10-28 06:16 log3.txt</div><div class="line">drwxr-xr-x 2 root root 4096 10-28 06:21 logs</div><div class="line">-rw-r--r-- 1 root root   29 10-28 06:05 test1.txt</div><div class="line">[root@localhost test3]<span class="comment"># mv -t /opt/soft/test/test4/ log1.txt log2.txt 	log3.txt </span></div><div class="line">[root@localhost test3]<span class="comment"># cd ..</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cd test4/</span></div><div class="line">[root@localhost test4]<span class="comment"># ll</span></div><div class="line">总计 12</div><div class="line">-rw-r--r-- 1 root root  8 10-28 06:15 log1.txt</div><div class="line">-rw-r--r-- 1 root root 12 10-28 06:15 log2.txt</div><div class="line">-rw-r--r-- 1 root root 13 10-28 06:16 log3.txt</div><div class="line">[root@localhost test4]<span class="comment">#</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明：</strong><br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;mv log1.txt log2.txt log3.txt test3 命令将log1.txt ，log2.txt， log3.txt 三个文件移到 test3目录中去，mv -t /opt/soft/test/test4/ log1.txt log2.txt log3.txt 命令又将三个文件移动到test4目录中去      </p>
<h3 id="实例4：将文件file1改名为file2，如果file2已经存在，则询问是否覆盖"><a href="#实例4：将文件file1改名为file2，如果file2已经存在，则询问是否覆盖" class="headerlink" title="实例4：将文件file1改名为file2，如果file2已经存在，则询问是否覆盖"></a>实例4：将文件file1改名为file2，如果file2已经存在，则询问是否覆盖</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令：</strong> </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mv -i log1.txt log2.txt</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">[root@localhost test4]<span class="comment"># ll</span></div><div class="line">总计 12</div><div class="line">-rw-r--r-- 1 root root  8 10-28 06:15 log1.txt</div><div class="line">-rw-r--r-- 1 root root 12 10-28 06:15 log2.txt</div><div class="line">-rw-r--r-- 1 root root 13 10-28 06:16 log3.txt</div><div class="line">[root@localhost test4]<span class="comment"># cat log1.txt </span></div><div class="line">odfdfs</div><div class="line">[root@localhost test4]<span class="comment"># cat log2.txt </span></div><div class="line">ererwerwer</div><div class="line">[root@localhost test4]<span class="comment"># mv -i log1.txt log2.txt </span></div><div class="line">mv：是否覆盖“log2.txt”? y</div><div class="line">[root@localhost test4]<span class="comment"># cat log2.txt </span></div><div class="line">odfdfs</div><div class="line">[root@localhost test4]<span class="comment">#</span></div></pre></td></tr></table></figure>
<h3 id="实例5：将文件file1改名为file2，即使file2存在，也是直接覆盖掉"><a href="#实例5：将文件file1改名为file2，即使file2存在，也是直接覆盖掉" class="headerlink" title="实例5：将文件file1改名为file2，即使file2存在，也是直接覆盖掉"></a>实例5：将文件file1改名为file2，即使file2存在，也是直接覆盖掉</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mv -f log3.txt log2.txt</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div></pre></td><td class="code"><pre><div class="line">[root@localhost test4]<span class="comment"># ll</span></div><div class="line">总计 8</div><div class="line">-rw-r--r-- 1 root root  8 10-28 06:15 log2.txt</div><div class="line">-rw-r--r-- 1 root root 13 10-28 06:16 log3.txt</div><div class="line">[root@localhost test4]<span class="comment"># cat log2.txt </span></div><div class="line">odfdfs</div><div class="line">[root@localhost test4]<span class="comment"># cat log3</span></div><div class="line">cat: log3: 没有那个文件或目录</div><div class="line">[root@localhost test4]<span class="comment"># ll</span></div><div class="line">总计 8</div><div class="line">-rw-r--r-- 1 root root  8 10-28 06:15 log2.txt</div><div class="line">-rw-r--r-- 1 root root 13 10-28 06:16 log3.txt</div><div class="line">[root@localhost test4]<span class="comment"># cat log2.txt </span></div><div class="line">odfdfs</div><div class="line">[root@localhost test4]<span class="comment"># cat log3.txt </span></div><div class="line">dfosdfsdfdss</div><div class="line">[root@localhost test4]<span class="comment"># mv -f log3.txt log2.txt </span></div><div class="line">[root@localhost test4]<span class="comment"># cat log2.txt </span></div><div class="line">dfosdfsdfdss</div><div class="line">[root@localhost test4]<span class="comment"># ll</span></div><div class="line">总计 4</div><div class="line">-rw-r--r-- 1 root root 13 10-28 06:16 log2.txt</div><div class="line">[root@localhost test4]<span class="comment">#</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明：</strong><br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;log3.txt的内容直接覆盖了log2.txt内容，-f 这是个危险的选项，使用的时候一定要保持头脑清晰，一般情况下最好不用加上它。        </p>
<h3 id="实例6：目录的移动"><a href="#实例6：目录的移动" class="headerlink" title="实例6：目录的移动"></a>实例6：目录的移动</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mv dir1 dir2</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div></pre></td><td class="code"><pre><div class="line">[root@localhost test4]<span class="comment"># ll</span></div><div class="line">-rw-r--r-- 1 root root 13 10-28 06:16 log2.txt</div><div class="line">[root@localhost test4]<span class="comment"># ll</span></div><div class="line">-rw-r--r-- 1 root root 13 10-28 06:16 log2.txt</div><div class="line">[root@localhost test4]<span class="comment"># cd ..</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">drwxr-xr-x 6 root root 4096 10-27 01:58 scf</div><div class="line">drwxrwxrwx 3 root root 4096 10-28 06:24 test3</div><div class="line">drwxr-xr-x 2 root root 4096 10-28 06:48 test4</div><div class="line">drwxr-xr-x 3 root root 4096 10-25 17:56 test5</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cd test3</span></div><div class="line">[root@localhost test3]<span class="comment"># ll</span></div><div class="line">drwxr-xr-x 2 root root 4096 10-28 06:21 logs</div><div class="line">-rw-r--r-- 1 root root   29 10-28 06:05 test1.txt</div><div class="line">[root@localhost test3]<span class="comment"># cd ..</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># mv test4 test3</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">drwxr-xr-x 6 root root 4096 10-27 01:58 scf</div><div class="line">drwxrwxrwx 4 root root 4096 10-28 06:54 test3</div><div class="line">drwxr-xr-x 3 root root 4096 10-25 17:56 test5</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cd test3/</span></div><div class="line">[root@localhost test3]<span class="comment"># ll</span></div><div class="line">drwxr-xr-x 2 root root 4096 10-28 06:21 logs</div><div class="line">-rw-r--r-- 1 root root   29 10-28 06:05 test1.txt</div><div class="line">drwxr-xr-x 2 root root 4096 10-28 06:48 test4</div><div class="line">[root@localhost test3]<span class="comment">#</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明：</strong><br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果目录dir2不存在，将目录dir1改名为dir2；否则，将dir1移动到dir2中。  </p>
<h3 id="实例7：移动当前文件夹下的所有文件到上一级目录"><a href="#实例7：移动当前文件夹下的所有文件到上一级目录" class="headerlink" title="实例7：移动当前文件夹下的所有文件到上一级目录"></a>实例7：移动当前文件夹下的所有文件到上一级目录</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mv * ../</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">[root@localhost test4]<span class="comment"># ll</span></div><div class="line">-rw-r--r-- 1 root root 25 10-28 07:02 log1.txt</div><div class="line">-rw-r--r-- 1 root root 13 10-28 06:16 log2.txt</div><div class="line">[root@localhost test4]<span class="comment"># mv * ../</span></div><div class="line">[root@localhost test4]<span class="comment"># ll</span></div><div class="line">[root@localhost test4]<span class="comment"># cd ..</span></div><div class="line">[root@localhost test3]<span class="comment"># ll</span></div><div class="line">-rw-r--r-- 1 root root   25 10-28 07:02 log1.txt</div><div class="line">-rw-r--r-- 1 root root   13 10-28 06:16 log2.txt</div><div class="line">drwxr-xr-x 2 root root 4096 10-28 06:21 logs</div><div class="line">-rw-r--r-- 1 root root   29 10-28 06:05 test1.txt</div><div class="line">drwxr-xr-x 2 root root 4096 10-28 07:02 test4</div></pre></td></tr></table></figure>
<h3 id="实例8：把当前目录的一个子目录里的文件移动到另一个子目录里"><a href="#实例8：把当前目录的一个子目录里的文件移动到另一个子目录里" class="headerlink" title="实例8：把当前目录的一个子目录里的文件移动到另一个子目录里"></a>实例8：把当前目录的一个子目录里的文件移动到另一个子目录里</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令：</strong> </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mv test3/*.txt test5</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出：</strong>   </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">drwxr-xr-x 6 root root 4096 10-27 01:58 scf</div><div class="line">drwxrwxrwx 4 root root 4096 10-28 07:02 test3</div><div class="line">drwxr-xr-x 3 root root 4096 10-25 17:56 test5</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cd test3</span></div><div class="line">[root@localhost test3]<span class="comment"># ll</span></div><div class="line">-rw-r--r-- 1 root root   25 10-28 07:02 log1.txt</div><div class="line">-rw-r--r-- 1 root root   13 10-28 06:16 log2.txt</div><div class="line">drwxr-xr-x 2 root root 4096 10-28 06:21 logs</div><div class="line">-rw-r--r-- 1 root root   29 10-28 06:05 test1.txt</div><div class="line">drwxr-xr-x 2 root root 4096 10-28 07:02 test4</div><div class="line">[root@localhost test3]<span class="comment"># cd ..</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># mv test3/*.txt test5</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cd test5</span></div><div class="line">[root@localhost test5]<span class="comment"># ll</span></div><div class="line">-rw-r--r-- 1 root root   25 10-28 07:02 log1.txt</div><div class="line">-rw-r--r-- 1 root root   13 10-28 06:16 log2.txt</div><div class="line">-rw-r--r-- 1 root root   29 10-28 06:05 test1.txt</div><div class="line">drwxr-xr-x 2 root root 4096 10-25 17:56 test5-1</div><div class="line">[root@localhost test5]<span class="comment"># 	cd ..</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cd test3/</span></div><div class="line">[root@localhost test3]<span class="comment"># ll</span></div><div class="line">drwxr-xr-x 2 root root 4096 10-28 06:21 logs</div><div class="line">drwxr-xr-x 2 root root 4096 10-28 07:02 test4</div><div class="line">[root@localhost test3]<span class="comment">#</span></div></pre></td></tr></table></figure>
<h3 id="实例9：文件被覆盖前做简单备份，前面加参数-b"><a href="#实例9：文件被覆盖前做简单备份，前面加参数-b" class="headerlink" title="实例9：文件被覆盖前做简单备份，前面加参数-b"></a>实例9：文件被覆盖前做简单备份，前面加参数-b</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令：</strong> </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mv log1.txt -b log2.txt</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">[root@localhost test5]<span class="comment"># ll</span></div><div class="line">-rw-r--r-- 1 root root   25 10-28 07:02 log1.txt</div><div class="line">-rw-r--r-- 1 root root   13 10-28 06:16 log2.txt</div><div class="line">-rw-r--r-- 1 root root   29 10-28 06:05 test1.txt</div><div class="line">drwxr-xr-x 2 root root 4096 10-25 17:56 test5-1</div><div class="line">[root@localhost test5]<span class="comment"># mv log1.txt -b log2.txt</span></div><div class="line">mv：是否覆盖“log2.txt”? y</div><div class="line">[root@localhost test5]<span class="comment"># ll</span></div><div class="line">-rw-r--r-- 1 root root   25 10-28 07:02 log2.txt</div><div class="line">-rw-r--r-- 1 root root   13 10-28 06:16 log2.txt~</div><div class="line">-rw-r--r-- 1 root root   29 10-28 06:05 test1.txt</div><div class="line">drwxr-xr-x 2 root root 4096 10-25 17:56 test5-1</div><div class="line">[root@localhost test5]<span class="comment">#</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明：</strong> </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;-b 不接受参数，mv会去读取环境变量VERSION_CONTROL来作为备份策略。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;–backup该选项指定如果目标文件存在时的动作，共有四种备份策略：   </p>
<ol>
<li>CONTROL=none或off : 不备份。    </li>
<li>CONTROL=numbered或t：数字编号的备份    </li>
<li>CONTROL=existing或nil：如果存在以数字编号的备份，则继续编号备份m+1…n：执行mv操作前已存在以数字编号的文件log2.txt.~1~，那么再次执行将产生log2.txt~2~，以次类推。如果之前没有以数字编号的文件，则使用下面讲到的简单备份。      </li>
<li>CONTROL=simple或never：使用简单备份：在被覆盖前进行了简单备份，简单备份只能有一份，再次被覆盖时，简单备份也会被覆盖。</li>
</ol>

	

	
		<span class="different-posts"><a href="/2017/10/12/2. Linux 命令/8. Linux 命令- mv/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
